<?php

namespace app\admin\controller;

use think\Request;

class Configs extends Common
{
    protected function initialize()
    {
        parent::initialize();
        $this->m = new \app\admin\model\Configs();   //别名：避免与控制名冲突
    }

    public function index()
    {
        $data=$this->m->select();
        $send=[];
        foreach ($data as $d){
            $send[$d['type']][$d['key']]=$d['value'];//格式化数据
        }
        $this->assign($send);
        return $this->fetch();
    }

    public function edit()
    {
        if($this->request->isPost()){
            $data=$this->request->post();
            $type=$data['type'];
            unset($data['type']);
            foreach ($data as $key=>$value){
                $config=\app\admin\model\Configs::where(['type'=>$type,'key'=>$key])->find();
                if($config){
                    $config->save(['value'=>$value]);
                }
            }
            return xjson('更新成功！');
        }

    }


}
